package com.cykj.service.impl;

import com.cykj.dto.ResponseDTO;
import com.cykj.entity.UserDO;
import com.cykj.mapper.UserMapper;
import com.cykj.service.IUserService;
import com.cykj.util.BcryptEncodeUtil;
import com.cykj.vo.UserVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * @Program: Mooc-Admin
 * @Package: com.cykj.service.impl
 * @Author: 张俊杰
 * @Create: 2025-07-09 21:22
 * @Description: TODO
 * @Vision: 1.0
 **/
@Service
public class UserServiceImpl implements IUserService {

    @Value("${baseUrl}")
    private String baseUrl;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BcryptEncodeUtil bcryptEncodeUtil;

    @Override
    public ResponseDTO getListUser(UserVO userVO) {

        PageHelper.startPage(userVO.getPageNum(), userVO.getPageSize());

        List<UserDO> listUser = userMapper.getListUser(userVO);

        PageInfo<UserDO> userDOPageInfo = new PageInfo<>(listUser);
        return ResponseDTO.success(userDOPageInfo);
    }

    @Override
    public ResponseDTO resetUserPassword(UserVO userVO) {

        String password = "123456";
        // 给密码也进行加密   拒绝明文存储
        userVO.setUserPassword(bcryptEncodeUtil.encode(password));
        Integer i = userMapper.resetUserPassword(userVO);

        try {
            if (i > 0) {
                return ResponseDTO.effectRow(i);
            }
        } catch (Exception e) {
            return ResponseDTO.error("重置失败");
        }
        return ResponseDTO.error("重置失败");
    }
}
